home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / syck / dumpers.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  11KB  |  357 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import _syck
  5.  
  6. try:
  7.     import cStringIO as StringIO
  8. except ImportError:
  9.     import StringIO
  10.  
  11. __all__ = [
  12.     'GenericDumper',
  13.     'Dumper',
  14.     'emit',
  15.     'dump',
  16.     'emit_documents',
  17.     'dump_documents']
  18.  
  19. class GenericDumper(_syck.Emitter):
  20.     
  21.     def dump(self, object):
  22.         self.emit(self._convert(object, { }))
  23.  
  24.     
  25.     def _convert(self, object, object_to_node):
  26.         if id(object) in object_to_node and self.allow_aliases(object):
  27.             return object_to_node[id(object)][1]
  28.         
  29.         node = self.represent(object)
  30.         object_to_node[id(object)] = (object, node)
  31.         if node.kind == 'seq':
  32.             for index in range(len(node.value)):
  33.                 item = node.value[index]
  34.                 node.value[index] = self._convert(item, object_to_node)
  35.             
  36.         elif node.kind == 'map':
  37.             if isinstance(node.value, dict):
  38.                 for key in node.value.keys():
  39.                     value = node.value[key]
  40.                     del node.value[key]
  41.                     node.value[self._convert(key, object_to_node)] = self._convert(value, object_to_node)
  42.                 
  43.             elif isinstance(node.value, list):
  44.                 for index in range(len(node.value)):
  45.                     (key, value) = node.value[index]
  46.                     node.value[index] = (self._convert(key, object_to_node), self._convert(value, object_to_node))
  47.                 
  48.             
  49.         
  50.         return node
  51.  
  52.     
  53.     def represent(self, object):
  54.         if isinstance(object, dict):
  55.             return _syck.Map(object.copy(), tag = 'tag:yaml.org,2002:map')
  56.         elif isinstance(object, list):
  57.             return _syck.Seq(object[:], tag = 'tag:yaml.org,2002:seq')
  58.         else:
  59.             return _syck.Scalar(str(object), tag = 'tag:yaml.org,2002:str')
  60.  
  61.     
  62.     def allow_aliases(self, object):
  63.         return True
  64.  
  65.  
  66.  
  67. class Dumper(GenericDumper):
  68.     INF = float('inf')
  69.     inf_value = repr(INF)
  70.     neginf_value = repr(-INF)
  71.     nan_value = repr(INF / INF)
  72.     
  73.     def find_representer(self, object):
  74.         for object_type in type(object).__mro__:
  75.             if object_type.__module__ == '__builtin__':
  76.                 name = object_type.__name__
  77.             else:
  78.                 name = '%s.%s' % (object_type.__module__, object_type.__name__)
  79.             method = 'represent_' + name.replace('.', '_')
  80.             if hasattr(self, method):
  81.                 return getattr(self, method)
  82.                 continue
  83.         
  84.  
  85.     
  86.     def represent(self, object):
  87.         representer = self.find_representer(object)
  88.         if representer:
  89.             return representer(object)
  90.         else:
  91.             return super(Dumper, self).represent(object)
  92.  
  93.     
  94.     def represent_object(self, object):
  95.         return _syck.Scalar(repr(object), tag = 'tag:yaml.org,2002:str')
  96.  
  97.     
  98.     def represent_NoneType(self, object):
  99.         return _syck.Scalar('~', tag = 'tag:yaml.org,2002:null')
  100.  
  101.     
  102.     def represent_bool(self, object):
  103.         return _syck.Scalar(repr(object), tag = 'tag:yaml.org,2002:bool')
  104.  
  105.     
  106.     def represent_str(self, object):
  107.         
  108.         try:
  109.             return _syck.Scalar(object.encode('ascii'), tag = 'tag:yaml.org,2002:str')
  110.         except UnicodeDecodeError:
  111.             
  112.             try:
  113.                 return _syck.Scalar(unicode(object, 'utf-8').encode('utf-8'), tag = 'tag:python.yaml.org,2002:str')
  114.             except UnicodeDecodeError:
  115.                 return _syck.Scalar(object.encode('base64'), tag = 'tag:yaml.org,2002:binary')
  116.             except:
  117.                 None<EXCEPTION MATCH>UnicodeDecodeError
  118.             
  119.  
  120.             None<EXCEPTION MATCH>UnicodeDecodeError
  121.  
  122.  
  123.     
  124.     def represent_unicode(self, object):
  125.         
  126.         try:
  127.             return _syck.Scalar(object.encode('ascii'), tag = 'tag:python.yaml.org,2002:unicode')
  128.         except UnicodeEncodeError:
  129.             return _syck.Scalar(object.encode('utf-8'), tag = 'tag:yaml.org,2002:str')
  130.  
  131.  
  132.     
  133.     def represent_list(self, object):
  134.         return _syck.Seq(object[:], tag = 'tag:yaml.org,2002:seq')
  135.  
  136.     
  137.     def represent_dict(self, object):
  138.         return _syck.Map(object.copy(), tag = 'tag:yaml.org,2002:map')
  139.  
  140.     
  141.     def represent_int(self, object):
  142.         return _syck.Scalar(repr(object), tag = 'tag:yaml.org,2002:int')
  143.  
  144.     
  145.     def represent_float(self, object):
  146.         value = repr(object)
  147.         if value == self.inf_value:
  148.             value = '.inf'
  149.         elif value == self.neginf_value:
  150.             value = '-.inf'
  151.         elif value == self.nan_value:
  152.             value = '.nan'
  153.         
  154.         return _syck.Scalar(value, tag = 'tag:yaml.org,2002:float')
  155.  
  156.     
  157.     def represent_complex(self, object):
  158.         if object.real != 0:
  159.             value = '%s+%sj' % (repr(object.real), repr(object.imag))
  160.         else:
  161.             value = '%sj' % repr(object.imag)
  162.         return _syck.Scalar(value, tag = 'tag:python.yaml.org,2002:complex')
  163.  
  164.     
  165.     def represent_sets_Set(self, object):
  166.         return _syck.Seq(list(object), tag = 'tag:yaml.org,2002:set')
  167.  
  168.     represent_set = represent_sets_Set
  169.     
  170.     def represent_datetime_datetime(self, object):
  171.         return _syck.Scalar(object.isoformat(), tag = 'tag:yaml.org,2002:timestamp')
  172.  
  173.     
  174.     def represent_long(self, object):
  175.         return _syck.Scalar(repr(object), tag = 'tag:python.yaml.org,2002:long')
  176.  
  177.     
  178.     def represent_tuple(self, object):
  179.         return _syck.Seq(list(object), tag = 'tag:python.yaml.org,2002:tuple')
  180.  
  181.     
  182.     def represent_type(self, object):
  183.         name = '%s.%s' % (object.__module__, object.__name__)
  184.         return _syck.Scalar('', tag = 'tag:python.yaml.org,2002:name:' + name)
  185.  
  186.     represent_classobj = represent_type
  187.     represent_class = represent_type
  188.     represent_function = represent_type
  189.     represent_builtin_function_or_method = represent_type
  190.     
  191.     def represent_module(self, object):
  192.         return _syck.Scalar('', tag = 'tag:python.yaml.org,2002:module:' + object.__name__)
  193.  
  194.     
  195.     def represent_instance(self, object):
  196.         cls = object.__class__
  197.         class_name = '%s.%s' % (cls.__module__, cls.__name__)
  198.         args = ()
  199.         state = { }
  200.         if hasattr(object, '__getinitargs__'):
  201.             args = object.__getinitargs__()
  202.         
  203.         if hasattr(object, '__getstate__'):
  204.             state = object.__getstate__()
  205.         elif not hasattr(object, '__getinitargs__'):
  206.             state = object.__dict__.copy()
  207.         
  208.         if not args and isinstance(state, dict):
  209.             return _syck.Map(state.copy(), tag = 'tag:python.yaml.org,2002:object:' + class_name)
  210.         
  211.         value = { }
  212.         if args:
  213.             value['args'] = list(args)
  214.         
  215.         if state or not isinstance(state, dict):
  216.             value['state'] = state
  217.         
  218.         return _syck.Map(value, tag = 'tag:python.yaml.org,2002:new:' + class_name)
  219.  
  220.     
  221.     def represent_object(self, object):
  222.         cls = type(object)
  223.         class_name = '%s.%s' % (cls.__module__, cls.__name__)
  224.         args = ()
  225.         state = { }
  226.         if cls.__reduce__ is type.__reduce__:
  227.             if hasattr(object, '__reduce_ex__'):
  228.                 reduce = object.__reduce_ex__(2)
  229.                 args = reduce[1][1:]
  230.             else:
  231.                 reduce = object.__reduce__()
  232.             if len(reduce) > 2:
  233.                 state = reduce[2]
  234.             
  235.             if state is None:
  236.                 state = { }
  237.             
  238.             if not args and isinstance(state, dict):
  239.                 return _syck.Map(state.copy(), tag = 'tag:python.yaml.org,2002:object:' + class_name)
  240.             
  241.             if not state and isinstance(state, dict):
  242.                 return _syck.Seq(list(args), tag = 'tag:python.yaml.org,2002:new:' + class_name)
  243.             
  244.             value = { }
  245.             if args:
  246.                 value['args'] = list(args)
  247.             
  248.             if state or not isinstance(state, dict):
  249.                 value['state'] = state
  250.             
  251.             return _syck.Map(value, tag = 'tag:python.yaml.org,2002:new:' + class_name)
  252.         else:
  253.             reduce = object.__reduce__()
  254.             cls = reduce[0]
  255.             class_name = '%s.%s' % (cls.__module__, cls.__name__)
  256.             args = reduce[1]
  257.             state = None
  258.             if len(reduce) > 2:
  259.                 state = reduce[2]
  260.             
  261.             if state is None:
  262.                 state = { }
  263.             
  264.             if not state and isinstance(state, dict):
  265.                 return _syck.Seq(list(args), tag = 'tag:python.yaml.org,2002:apply:' + class_name)
  266.             
  267.             value = { }
  268.             if args:
  269.                 value['args'] = list(args)
  270.             
  271.             if state or not isinstance(state, dict):
  272.                 value['state'] = state
  273.             
  274.             return _syck.Map(value, tag = 'tag:python.yaml.org,2002:apply:' + class_name)
  275.  
  276.     
  277.     def represent__syck_Node(self, object):
  278.         object_type = type(object)
  279.         type_name = '%s.%s' % (object_type.__module__, object_type.__name__)
  280.         state = []
  281.         if hasattr(object_type, '__slotnames__'):
  282.             for name in object_type.__slotnames__:
  283.                 value = getattr(object, name)
  284.                 if value:
  285.                     state.append((name, value))
  286.                     continue
  287.             
  288.         
  289.         return _syck.Map(state, tag = 'tag:python.yaml.org,2002:object:' + type_name)
  290.  
  291.     
  292.     def allow_aliases(self, object):
  293.         if object is None or type(object) in [
  294.             int,
  295.             bool,
  296.             float]:
  297.             return False
  298.         
  299.         if type(object) is str:
  300.             if not object or object.isalnum():
  301.                 return False
  302.             
  303.         if type(object) is tuple and not object:
  304.             return False
  305.         
  306.         return True
  307.  
  308.  
  309.  
  310. def emit(node, output = None, Dumper = Dumper, **parameters):
  311.     if output is None:
  312.         dumper = Dumper(StringIO.StringIO(), **parameters)
  313.     else:
  314.         dumper = Dumper(output, **parameters)
  315.     dumper.emit(node)
  316.     if output is None:
  317.         return dumper.output.getvalue()
  318.     
  319.  
  320.  
  321. def dump(object, output = None, Dumper = Dumper, **parameters):
  322.     if output is None:
  323.         dumper = Dumper(StringIO.StringIO(), **parameters)
  324.     else:
  325.         dumper = Dumper(output, **parameters)
  326.     dumper.dump(object)
  327.     if output is None:
  328.         return dumper.output.getvalue()
  329.     
  330.  
  331.  
  332. def emit_documents(nodes, output = None, Dumper = Dumper, **parameters):
  333.     if output is None:
  334.         dumper = Dumper(StringIO.StringIO(), **parameters)
  335.     else:
  336.         dumper = Dumper(output, **parameters)
  337.     for node in nodes:
  338.         dumper.emit(node)
  339.     
  340.     if output is None:
  341.         return dumper.output.getvalue()
  342.     
  343.  
  344.  
  345. def dump_documents(objects, output = None, Dumper = Dumper, **parameters):
  346.     if output is None:
  347.         dumper = Dumper(StringIO.StringIO(), **parameters)
  348.     else:
  349.         dumper = Dumper(output, **parameters)
  350.     for object in objects:
  351.         dumper.dump(object)
  352.     
  353.     if output is None:
  354.         return dumper.output.getvalue()
  355.     
  356.  
  357.